<?php 
class ControllerPhoneNumber extends Controller {
	private $error = array(); 

	public function index() {
		$this->language->load('phone/number');

		$this->document->setTitle($this->language->get('heading_title')); 

		$this->load->model('phone/number');

		$this->getList();
	}

	public function update() {
		$this->language->load('phone/number');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('phone/number');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_phone_number->editNumber($this->request->get['phone_id'], $this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';

			if (isset($this->request->get['filter_name'])) {
				$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
			}

			if (isset($this->request->get['filter_model'])) {
				$url .= '&filter_model=' . urlencode(html_entity_decode($this->request->get['filter_model'], ENT_QUOTES, 'UTF-8'));
			}

			if (isset($this->request->get['filter_price'])) {
				$url .= '&filter_price=' . $this->request->get['filter_price'];
			}

			if (isset($this->request->get['filter_quantity'])) {
				$url .= '&filter_quantity=' . $this->request->get['filter_quantity'];
			}	

			if (isset($this->request->get['filter_status'])) {
				$url .= '&filter_status=' . $this->request->get['filter_status'];
			}

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			$this->redirect($this->url->link('phone/number', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}
		
		$this->data['phone'] = $this->model_phone_number->getNumber($this->request->get['phone_id']);
		$this->data['total'] = ceil($this->data['phone']['total_comments'] / 20);

		$this->getForm();
	}

	public function delete() {
		$this->language->load('phone/number');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('phone/number');

		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $phone_id) {
				$this->model_phone_number->deleteNumber($phone_id);
			}

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';

			if (isset($this->request->get['filter_name'])) {
				$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
			}

			if (isset($this->request->get['filter_model'])) {
				$url .= '&filter_model=' . urlencode(html_entity_decode($this->request->get['filter_model'], ENT_QUOTES, 'UTF-8'));
			}

			if (isset($this->request->get['filter_price'])) {
				$url .= '&filter_price=' . $this->request->get['filter_price'];
			}

			if (isset($this->request->get['filter_quantity'])) {
				$url .= '&filter_quantity=' . $this->request->get['filter_quantity'];
			}	

			if (isset($this->request->get['filter_status'])) {
				$url .= '&filter_status=' . $this->request->get['filter_status'];
			}

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			$this->redirect($this->url->link('phone/number', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

		$this->getList();
	}

	public function json(){
		$json = array();
		
		$this->load->model('phone/comment');
		
		$page = 1;
		if(isset($this->request->get['page']) && is_numeric($this->request->get['page']))
			$page = $this->request->get['page'];
		
		$data = array(
				'phone_id' => $this->request->get['phone_id'], 
				'start' => $page - 1 
		);
		
		$json = $this->model_phone_comment->getComments($data);
		
		$this->response->setOutput(json_encode($json));
	}
	
	protected function getList() {
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = '';
		}
		
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}

		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		$url = '';

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => false
		);

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('phone/number', 'token=' . $this->session->data['token'] . $url, 'SSL'),       		
			'separator' => ' :: '
		);

		$this->data['insert'] = $this->url->link('phone/number/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
		$this->data['copy'] = $this->url->link('phone/number/copy', 'token=' . $this->session->data['token'] . $url, 'SSL');	
		$this->data['delete'] = $this->url->link('phone/number/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');

		$this->data['numbers'] = array();

		$data = array(
			'sort'            => $sort,
			'order'           => $order,
			'start'           => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit'           => $this->config->get('config_admin_limit')
		);

		$this->load->model('tool/image');

		$product_total = $this->model_phone_number->getTotalNumbers($data);

		$results = $this->model_phone_number->getNumbers($data);

		foreach ($results as $result) {
			$action = array();

			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('phone/number/update', 'token=' . $this->session->data['token'] . '&phone_id=' . $result['phone_id'] . $url, 'SSL')
			);
			
			$this->data['numbers'][] = array(
				'phone_id' => $result['phone_id'],
				'number'       => $result['number'],
				'location'      => $result['location'],
				'total_comments'      => $result['total_comments'],
				'total_search'    => $result['total_search'],
				'created'      => $result['created'],
				'selected'           => isset($this->request->post['selected']) && in_array($result['phone_id'], $this->request->post['selected']),
				'action'     => $action
			);
		}

		$this->data['heading_title'] = $this->language->get('heading_title');		

		$this->data['text_enabled'] = $this->language->get('text_enabled');		
		$this->data['text_disabled'] = $this->language->get('text_disabled');		
		$this->data['text_no_results'] = $this->language->get('text_no_results');		
		$this->data['text_image_manager'] = $this->language->get('text_image_manager');		

		$this->data['column_name'] = $this->language->get('column_name');
		$this->data['column_location'] = $this->language->get('column_location');
		$this->data['column_total_search'] = $this->language->get('column_total_search');
		$this->data['column_total_comments'] = $this->language->get('column_total_comments');
		$this->data['column_action'] = $this->language->get('column_action');

		$this->data['button_copy'] = $this->language->get('button_copy');		
		$this->data['button_insert'] = $this->language->get('button_insert');		
		$this->data['button_delete'] = $this->language->get('button_delete');		
		$this->data['button_filter'] = $this->language->get('button_filter');

		$this->data['token'] = $this->session->data['token'];

		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];

			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$url = '';

		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		$this->data['sort_number'] = $this->url->link('phone/number', 'token=' . $this->session->data['token'] . '&sort=p.number' . $url, 'SSL');
		$this->data['sort_total_comments'] = $this->url->link('phone/number', 'token=' . $this->session->data['token'] . '&sort=p.total_comments' . $url, 'SSL');
		$this->data['sort_total_search'] = $this->url->link('phone/number', 'token=' . $this->session->data['token'] . '&sort=p.total_search' . $url, 'SSL');

		$url = '';

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		$pagination = new Pagination();
		$pagination->total = $product_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('phone/number', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');

		$this->data['pagination'] = $pagination->render();
		$this->data['sort'] = $sort;
		$this->data['order'] = $order;

		$this->template = 'phone/number_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);

		$this->response->setOutput($this->render());
	}

	protected function getForm() {
		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['tab_data'] = $this->language->get('tab_data');
		$this->data['tab_comments'] = $this->language->get('tab_comments');
		$this->data['text_number'] = $this->language->get('text_number');
		$this->data['text_location'] = $this->language->get('text_location');
		$this->data['text_total_comments'] = $this->language->get('text_total_comments');
		$this->data['text_total_search'] = $this->language->get('text_total_search');
		$this->data['text_created'] = $this->language->get('text_created');
		$this->data['page'] = $this->language->get('text_page');
		
		$this->data['column_nick'] = $this->language->get('column_nick');
		$this->data['column_comment'] = $this->language->get('column_comment');
		$this->data['column_call_type'] = $this->language->get('column_call_type');
		$this->data['column_ip'] = $this->language->get('column_ip');
		$this->data['column_rate'] = $this->language->get('column_rate');
		$this->data['column_created'] = $this->language->get('column_created');
		
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');

		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		$url = '';

		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => false
		);

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('phone/number', 'token=' . $this->session->data['token'] . $url, 'SSL'),
			'separator' => ' :: '
		);

		if (!isset($this->request->get['phone_id'])) {
			$this->data['action'] = $this->url->link('phone/number/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
		} else {
			$this->data['action'] = $this->url->link('phone/number/update', 'token=' . $this->session->data['token'] . '&phone_id=' . $this->request->get['phone_id'] . $url, 'SSL');
		}

		$this->data['cancel'] = $this->url->link('phone/number', 'token=' . $this->session->data['token'] . $url, 'SSL');

		$this->data['token'] = $this->session->data['token'];

		$this->load->model('localisation/language');

		$this->template = 'phone/number_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);

		$this->response->setOutput($this->render());
	}

	protected function validateForm() {
		if (!$this->user->hasPermission('modify', 'phone/number')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		if ((utf8_strlen($this->request->post['number']) < 1) || (utf8_strlen($this->request->post['number']) > 64)) {
			$this->error['number'] = $this->language->get('error_model');
		}
		
		if ($this->error && !isset($this->error['warning'])) {
			$this->error['warning'] = $this->language->get('error_warning');
		}

		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}

	protected function validateDelete() {
		if (!$this->user->hasPermission('modify', 'phone/number')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}

	protected function validateCopy() {
		if (!$this->user->hasPermission('modify', 'phone/number')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}
}
?>